class Solution {
public:
    vector<int> canSeePersonsCount(vector<int>& heights) {
        stack<int> a;
        vector<int> b(heights.size());
        a.push(heights[heights.size()-1]);
        b[heights.size()-1]=0;
        for(int i=heights.size()-2;i>=0;i--){
            int ans=0;
             if(heights[i]<=a.top()){
                a.push(heights[i]);
                ans++;
             }
             else{
                while(!a.empty()&&heights[i]>a.top()){
                    a.pop();
                    ans++;
                }
                if(!a.empty()){
                    ans++;
                }
                
                a.push(heights[i]);
             }
             b[i]=ans;
        }
        return b;
    }
};
